﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;
using ThietBiSo.UrlReview;

namespace ThietBiSo.Controllers
{
    public class xaydungController : ApplicationController
    {

        XayDung tblxd = new XayDung();
        MXayDung mxd = new MXayDung();
        MXayDung_DanhMuc mdm = new MXayDung_DanhMuc();
        SEOUrl seo = new SEOUrl();
        #region website
      

      

        #endregion

        #region Admin

        public ActionResult xaydung()
        {

            ViewData["xaydungs"] = mxd.FindAll();
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /quangcao/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                // TODO: Add insert logic here
                //UploadFile();

                tblxd.HinhAnh = collection.Get("HinhAnh");
                tblxd.MaDanhMucXD = int.Parse(collection.Get("ddlDanhMucXayDung"));

                tblxd.TieuDe = collection.Get("TieuDe");
                tblxd.NoiDungTomTat = collection.Get("NoiDungTomTat");
                tblxd.ChiTiet = collection.Get("ChiTiet");

                tblxd.HienThi = true;
                tblxd.TrangChu = false;
                if (mxd.GetMaxTT() == 0)
                {
                    tblxd.ThuTu = 0;
                }
                else
                    tblxd.ThuTu = mxd.GetMaxTT() + 1;
                mxd.Save(tblxd);
                return RedirectToAction("xaydung");
            }
            catch
            {
                return View();
            }
        }

        // GET: /xay dung/Edit/5

        public ActionResult Edit(int id)
        {
            tblxd = mxd.GetByID(id);
            ViewData["xaydungs"] = tblxd;
            return View();
        }

        //
        // POST: /xay dung/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {

                tblxd.MaXayDung = id;

                if (collection.Get("HinhAnh") == null || collection.Get("HinhAnh") == "")
                {
                    tblxd.HinhAnh = collection.Get("HiddenHinhAnh");
                }
                else
                {
                    tblxd.HinhAnh = collection.Get("HinhAnh");
                }

                tblxd.MaDanhMucXD = int.Parse(collection.Get("ddlDanhMucXayDung"));
                tblxd.TieuDe = collection.Get("TieuDe");
                tblxd.NoiDungTomTat = collection.Get("NoiDungTomTat");
                tblxd.ChiTiet = collection.Get("ChiTiet");
                tblxd.ThuTu = int.Parse(collection.Get("ThuTu"));
               
                mxd.Update(tblxd);
                return RedirectToAction("xaydung");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int ma)
        {

            string error = mxd.Delete(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult HienThi(int ma)
        {

            string error = mxd.HienThi(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TrangChu(int ma)
        {

            string error = mxd.TrangChu(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }
        
        #endregion


    }
}
